<script type="text/javascript">
function isValidEmail(emailAddress) {
	   
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
 
    return pattern.test(emailAddress);
 
    }
function check()
  {
      //alert(jQuery("#name_newletter").val().length);   
      var result=true;
	  if(jQuery("#name_newletter").val()=='' || jQuery("#name_newletter").val()=='...') 
	  { 
	    	 jQuery("#name_newletter").attr('style','border:1px solid red');
	    	 result=false
	    	 
	  }
	  else
		  jQuery("#name_newletter").attr('style','border:0px solid red');

	  if(!isValidEmail(jQuery("#email_newletter").val())) 
	  { 
	    	 jQuery("#email_newletter").attr('style','border:1px solid red');
	    	 result=false
	    	 
	  }
	 return result;
	  
	  //jQuery('#form_newletter').submit();    
  }
</script>
<?php 
$db = &JFactory::getDBo(); 
$name=JRequest::getVar('name_newletter');
 $email=JRequest::getVar('email_newletter'); 
 if(isset($_POST['send']))
  {
  	 //echo "ddd" ;die();
  	 
  	 $list=JRequest::getVar('list_newletter');
  	 $company=JRequest::getVar('company_newletter');
  	 $time=mktime();
     
  	 $query="insert into #__acymailing_subscriber(email,name,created,confirmed,enabled,accept,company)
  	 values('".$email."','".$name."',".$time.",1,1,1,'".$company."')";
  	 //echo $query ;die(); 
  	 $db->setQuery($query);	
  	 $db->query();
  	 
  	 $id_sub=$db->insertid();
  	 $query="insert into #__acymailing_listsub(listid,subid,subdate,status)
  	 values(1,".$id_sub.",".$time.",1)";
  	 $db->setQuery($query);	
  	 $db->query();
  	 header("Location:".JURI::base()."index.php?option=com_content&view=article&id=65");
  	 
  }
  elseif(isset($_POST['Unsubscribe'])) 
  {
  	 $query="select * from #__acymailing_subscriber where email='".$email."' and name='".$name."' ";
  	 $db->setQuery($query);
  	 $id_sub=$db->loadObject()->subid;
  	 if(count($db->loadObject())==0)
  	 {
  	 	?>
  	 	<script>
  	 	  jQuery('#alert').html('Name or email wrong . Please check again');
		  jQuery('#backoverlay').show();
		  jQuery('#show_popup').show();
  	 	</script>
  	 	<?php 
  	 }
  	 else
  	 {
  	   $query="delete from #__acymailing_subscriber where subid=".$id_sub." ";
  	   $db->setQuery($query);	
  	   $db->query();
  	   $query="delete from #__acymailing_listsub where subid=".$id_sub." ";
  	   $db->setQuery($query);	
  	   $db->query();
  	   header("Location:".JURI::base()."index.php?option=com_content&view=article&id=131");
  	   
  	 } 
  }
?>
<div id="custom-page-heading">
	<h1 class="heading">NEWSLETTER</h1>
</div>
	<?php
		$article = JUtility::getArticle(107);
		echo $article->introtext;
	?>
                  
   <div class="tpl-form gradient-1 clearfix">
  <form action="index.php?option=com_acymailing&view=user&layout=modify" id="form_newletter" method="post" onsubmit="return check();">
                            <div class="input-block ip-col-2">
                                <label for="">Name *</label>
                                <input type="text" class="f-input i-toggle" value="..." name="name_newletter" id="name_newletter">
                            </div>
                            <div class="input-block ip-col-2 col-last">
                                <label for="">E-mail *</label>
                                <input type="text" class="f-input i-toggle" value="..." name="email_newletter" id="email_newletter">
                            </div>
                            <div class="input-block ip-col-2" style="display:none;">
                                <label for="">Company</label>
                                <input type="text" class="f-input i-toggle" value="" name="company_newletter" id="company_newletter">
                            </div>
                            <div class="input-block ip-col-2 col-last" style="display:none;">
                                <label for="">Interest</label>
                                 <?php 
					                
									$db = &JFactory::getDBo();
									$query="SELECT * FROM #__acymailing_list";
									//echo $query ;die();
									$db->setQuery($query);	
									$list= $db->loadObjectList();
									
								?>
                                <select class="f-select" name="list_newletter" id="list_newletter">
                                    <?php for($i=0;$i<count($list);$i++){?>
                                      <option value="<?php echo $list[$i]->listid?>"><?php echo $list[$i]->name?></option>
                                    <?php }?>
                                   
                                </select>
                            </div>
                            <div class="clr"></div>
                            <div class="form-submit clearfix">
                                <input style="display:none;" type="submit" value="Send" name="send" id="send"/>  
                                <input style="display:none;" type="submit" value="Unsubscribe" name="Unsubscribe" id="Unsubscribe"/>
                                  <a  href="#" class="btnUnsubscribe" onclick="jQuery('#Unsubscribe').click();">Unsubscribe</a>
                                  <a  href="#" class="btnSubscribe" onclick="jQuery('#send').click();">Send</a> 
                            </div>
                           
                        </form>
      </div>  
<?php
return;
/**
 * @package	Acymailing for Joomla!
 * @version	4.0.1
 * @author	acyba.com
 * @copyright	(C) 2009-2012 ACYBA S.A.RL. All rights reserved.
 * @license	GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');
?><div id="acymodifyform">
<?php if($this->values->show_page_heading){ ?>
<h1 class="contentheading<?php echo $this->values->suffix; ?>"><?php echo $this->values->page_heading; ?></h1>
<?php } ?>
<?php if(!empty($this->introtext)){ echo '<span class="acymailing_introtext">'.$this->introtext.'</span>'; } ?>
<form action="<?php echo JRoute::_( 'index.php' );?>" method="post" name="adminForm" id="adminForm" <?php if(!empty($this->fieldsClass->formoption)) echo $this->fieldsClass->formoption; ?> >
	<fieldset class="adminform">
		<legend><span><?php echo JText::_( 'USER_INFORMATIONS' ); ?></span></legend>
		<table cellspacing="1" align="center" width="100%" id="acyuserinfo">
		<?php if(acymailing_level(3)){
				foreach($this->extraFields as $fieldName => $oneExtraField) {
					echo '<tr id="tr'.$fieldName.'"><td width="150" class="key">'.$this->fieldsClass->getFieldName($oneExtraField).'</td><td>';
					if(in_array($fieldName,array('name','email')) AND !empty($this->subscriber->userid)){echo $this->subscriber->$fieldName; }
					else{echo $this->fieldsClass->display($oneExtraField,@$this->subscriber->$fieldName,'data[subscriber]['.$fieldName.']'); }
					echo '</td></tr>';
				}
			}else{ ?>
			<tr id="trname">
				<td width="150" class="key">
					<label for="field_name">
					<?php echo JText::_( 'JOOMEXT_NAME' ); ?>
					</label>
				</td>
				<td>
				<?php
				if(empty($this->subscriber->userid)){
						echo '<input type="text" name="data[subscriber][name]" id="field_name" class="inputbox" style="width:200px;" value="'.$this->escape(@$this->subscriber->name).'" />';
				}else{
					echo $this->subscriber->name;
				}
				?>
				</td>
			</tr>
			<tr id="tremail">
				<td class="key">
					<label for="field_email">
					<?php echo JText::_( 'JOOMEXT_EMAIL' ); ?>
					</label>
				</td>
				<td>
					<?php
					if(empty($this->subscriber->userid)){
						echo '<input class="inputbox" type="text" name="data[subscriber][email]" id="field_email" style="width:200px;" value="'.$this->escape(@$this->subscriber->email).'" />';
					}else{
						echo $this->subscriber->email;
					}
					?>
				</td>
			</tr>
			<tr id="trhtml">
				<td class="key">
					<?php echo JText::_( 'RECEIVE' ); ?>
				</td>
				<td>
					<?php echo JHTML::_('acyselect.booleanlist', "data[subscriber][html]" , '',$this->subscriber->html,JText::_('HTML'),JText::_('JOOMEXT_TEXT'),'user_html'); ?>
				</td>
			</tr>
		<?php }
		if(empty($this->subscriber->subid) AND $this->config->get('captcha_enabled')){ ?>
			<tr id="trcaptcha">
				<td class="captchakeycomponent">
					<img title="<?php echo JText::_('ERROR_CAPTCHA'); ?>" width="<?php echo $this->config->get('captcha_width_component') ?>" height="<?php echo $this->config->get('captcha_height_component') ?>" class="captchaimagecomponent" src="<?php if(ACYMAILING_J16){ echo JRoute::_('index.php?option=com_acymailing&ctrl=captcha&val='.rand(0,10000).'&no_html=1'); }else{ echo rtrim(JURI::root(),'/').'/index.php?option=com_acymailing&amp;ctrl=captcha&amp;val='.rand(0,10000).'&amp;no_html=1';} ?>" alt="captcha" />
				</td>
				<td class="captchafieldcomponent">
					<input title="<?php echo JText::_('ERROR_CAPTCHA'); ?>" id="user_captcha" class="inputbox captchafield" type="text" name="acycaptcha" style="width:50px" />
				</td>
			</tr>
			<?php }
			?>
		</table>
	</fieldset>
	<?php if($this->displayLists){?>
	<fieldset class="adminform">
		<legend><span><?php echo JText::_( 'SUBSCRIPTION' ); ?></span></legend>
		<table cellspacing="1" align="center" width="100%" id="acyusersubscription">
			<thead>
				<tr>
					<th  nowrap="nowrap" align="center" width="150">
					<?php echo JText::_( 'SUBSCRIBE' );?>
					</th>
					<th  nowrap="nowrap" align="center">
					<?php echo JText::_( 'LIST' ); ?>
					</th>
				</tr>
			</thead>
			<tbody>
				<?php
				$k = 0;
				foreach($this->subscription as $row){
					if(empty($row->published) OR !$row->visible) continue;
					?>
				<tr class="<?php echo "row$k"; ?>">
					<td align="center" valign="top" class="acystatus">
						<span><?php echo $this->status->display("data[listsub][".$row->listid."][status]",@$row->status); ?></span>
					</td>
					<td valign="top">
						<div class="list_name"><?php echo $row->name ?></div>
						<div class="list_description"><?php echo $row->description ?></div>
					</td>
				</tr>
				<?php
					$k = 1 - $k;
				} ?>
			</tbody>
		</table>
	</fieldset>
	<?php } ?>
	<br/>
	<input type="hidden" name="option" value="<?php echo ACYMAILING_COMPONENT; ?>" />
	<input type="hidden" name="task" value="savechanges" />
	<input type="hidden" name="ctrl" value="user" />
	<?php echo JHTML::_( 'form.token' ); ?>
	<input type="hidden" name="subid" value="<?php echo $this->subscriber->subid; ?>" />
	<?php if(JRequest::getCmd('tmpl') == 'component'){ ?><input type="hidden" name="tmpl" value="component" /><?php } ?>
	<input type="hidden" name="key" value="<?php echo $this->subscriber->key; ?>" />
	<p class="acymodifybutton">
		<input class="button btn btn-primary" type="submit" onclick="return checkChangeForm();" value="<?php echo empty($this->subscriber->subid) ? JText::_('SUBSCRIBE',true) : JText::_('SAVE_CHANGES',true)?>"/>
	</p>
</form>
<?php if(!empty($this->finaltext)){ echo '<span class="acymailing_finaltext">'.$this->finaltext.'</span>'; } ?>
</div>
